using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data;
using System.Data.Entity.Infrastructure;
using ShopPowerTemplate.Services.Properties;
using ShopPowerTemplate.Entities;

namespace ShopPowerTemplate.Services
{
    public class ProductServiceBase : Service
    {
		public ProductServiceBase(IEntitiesDB entitiesDB) : base(entitiesDB) { }
		
        public virtual Int32 Add(Product product)
        {
            product.Subcategory = EntitiesDB.EntryWithState(product.Subcategory, EntityState.Unchanged);
            EntitiesDB.ProductSet.Add(product);
            EntitiesDB.SaveChanges();
            return product.Id;
        }

        public virtual void Change(Product product)
        {
            product.Subcategory = EntitiesDB.EntryWithState(product.Subcategory, EntityState.Unchanged);
		    EntitiesDB.Entry(product).State = EntityState.Modified;

            EntitiesDB.SaveChanges();
        }
		
		public virtual void Remove(int id)
        {
			Product product = Find(id);
            try
            {
                EntitiesDB.ProductSet.Remove(product);
                EntitiesDB.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw new RemoveException(string.Format(Resources.RemoveException, product.ToString()));
            }
        }

        public virtual void Remove(Product product)
        {
			Remove(product.Id);
        }        

        public virtual Product Find(int id)
        {
			Product product = 
				EntitiesDB.ProductSet
					.Include("Subcategory")
					.Include("Images")
					.Include("Reviews")
					.Include("ProductDetails")
					.FirstOrDefault(x => x.Id == id);
			
			if(product!=null)
			{
				var subcategoryAux = product.Subcategory; //EF4.1 bug hack
			}
			return product;
        }

        public virtual List<Product> FindAll()
        {
            return EntitiesDB.ProductSet.OrderBy(x => x.Description).ToList();
        }
		
		public virtual List<Product> Search(string searchValue)
        {
            return Search(searchValue, string.Empty, true, 0, 0);
        }

        public virtual List<Product> Search(string searchValue, int pageIndex, int pageSize)
        {
            return Search(searchValue, string.Empty, true, pageIndex, pageSize);
        }

        public virtual List<Product> Search(string searchValue, string orderBy)
        {
            return Search(searchValue, orderBy, true, 0, 0);
        }

        public virtual List<Product> Search(string searchValue, string orderBy, int pageIndex, int pageSize)
        {
            return Search(searchValue, orderBy, true, pageIndex, pageSize);
        }

        public virtual List<Product> Search(string searchValue, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize = 10)
        {
            IQueryable<Product> query = EntitiesDB.ProductSet.Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)
				|| x.Description.Contains(searchValue));
			
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }
		
		public virtual List<Product> Search(Expression<Func<Product, bool>> searchCriteria, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize=10)
        {
            IQueryable<Product> query = EntitiesDB.ProductSet.Where(searchCriteria);
            
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int SearchCount(string searchValue)
        {
            return EntitiesDB.ProductSet.Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)
				|| x.Description.Contains(searchValue)).Count();
        }
		
		public virtual List<Product> FindAvailableProductList<TKey>(List<Product> notAvailableList, string searchValue, System.Linq.Expressions.Expression<Func<Product, TKey>> orderBy, bool ascending, int pageIndex, int pageSize)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            IQueryable<Product> query = EntitiesDB.ProductSet.Where(a => !notAvailableIDList.Any(id => id == a.Id)).Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)
				|| x.Description.Contains(searchValue));

			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int FindAvailableProductListCount(List<Product> notAvailableList, string searchValue)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            return EntitiesDB.ProductSet.Where(a => !notAvailableIDList.Any(id => id == a.Id)).Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)
				|| x.Description.Contains(searchValue)).Count();
        }
	
		public virtual void AddImages(Product product, Image images)
        {
			product = Find(product.Id);
            images.Product = product;
            product.Images.Add(images);

            Change(product);
        }
		
		public virtual void RemoveImages(Product product, Image images)
        {
            product.Images.Remove(images);
			EntitiesDB.ImageSet.Remove(images);			
            Change(product);
        }

		public virtual void AddReviews(Product product, Review reviews)
        {
			product = Find(product.Id);
            reviews.Product = product;
            product.Reviews.Add(reviews);
            reviews.Customer = EntitiesDB.EntryWithState(reviews.Customer, EntityState.Unchanged);

            Change(product);
        }
		
		public virtual void RemoveReviews(Product product, Review reviews)
        {
            product.Reviews.Remove(reviews);
			EntitiesDB.ReviewSet.Remove(reviews);			
            Change(product);
        }

		public virtual void AddProductDetails(Product product, ProductDetail productDetails)
        {
			product = Find(product.Id);
            productDetails.Product = product;
            product.ProductDetails.Add(productDetails);

            Change(product);
        }
		
		public virtual void RemoveProductDetails(Product product, ProductDetail productDetails)
        {
            product.ProductDetails.Remove(productDetails);
			EntitiesDB.ProductDetailSet.Remove(productDetails);			
            Change(product);
        }

    }
}
